var lyricmasterFetcher= {fetch_lyr: function (artist, track, cbFn) {

	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.lyricmaster.");
        prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
        var lm_libStyle = prefs.getCharPref("libStyle");
        var lm_webLoc = prefs.getCharPref("webLoc");
	
	if (lm_libStyle == "DOEJ"){
	   var edit_artist = artist.split(", ");
	   artist = edit_artist[1] + " " +edit_artist[0];
	   }
	edit_artist = artist.split(" (");
	var search_artist = artist;
	if (edit_artist.length>0)
	{
		search_artist = edit_artist[0];
	}
	var search_track = track;
	var edit_track = track.split(" (");
	if (edit_track.length>0)
	{
		search_track = edit_track[0];
	}
	var req = new XMLHttpRequest();
	var _artist = encodeURIComponent(artist);
	var _search_track = encodeURIComponent(search_track);

	if (lm_webLoc == "LWIKI"){
	   var url = "http://lyricwiki.org/api.php?artist=" + _artist + "&song=" + _search_track + "&fmt=text";
	}

	if (lm_webLoc == "LPLUGIN"){
	   var url = "http://www.lyricsplugin.com/wmplayer03/plugin/?title=" + _search_track + "&artist=" + _artist;
	}



	req.open("GET", url, true);
	req.onreadystatechange = function()
	{
		if (this.readyState != 4)
		{
			return;
		}
		if (this.status == 200)
		{
			var respLyr = this.responseText;				

			if (lm_webLoc == "LPLUGIN")
			{
				// Initial lookup is done, now emulate
				// the lyricsplugin.com js redirect
				// making sure to include the timestamp
				// and ID
        			var jsredirect = respLyr.split("javascript:getContent");
        			var inputargchunk = jsredirect[1].split("\)");
        			var inputargs = inputargchunk[0].replace(/'/g, "")
                                        			.replace(/\(/g, "")
                                        			.replace(/ /g, "");
        			var params = inputargs.split(",");
        			var artist = params[0];
        			var title = params[1];
        			var timestamp = params[2];
        			var id = params[3];

        			var url = "http://www.lyricsplugin.com/wmplayer03/plugin/content.php?"
                        			+ "artist=" + artist
                        			+ "&title=" + title
                        			+ "&time=" + timestamp
                        			+ "&hash=" + id;
        			// The referer must be spoofed otherwise lyrics will not be
        			// included in the response
        			var referer = "http://www.lyricsplugin.com/wmplayer03/plugin/?"
                        			+ "title=" + title + "&artist=" + artist;

        			req.open("GET", url, true);
        			req.setRequestHeader("Referer", referer);
        			req.onreadystatechange = function() {
					if (this.readyState != 4)
						return;

                			if (this.readyState == 4 || this.readyState == "complete")
                			{
                        			var respLyr = this.responseText;
                               			respLyrResult = respLyr.split("<div id=\"lyrics\">");
                               			lyrDiv = respLyrResult[1].split("</div>");
                               			respLyr = lyrDiv[0];
                               			respLyr = respLyr.replace(/\n/, "");
		   				respLyr = respLyr.replace(/<br \/>/g,"");
						respLyr = respLyr.replace(/<a href=\"http:\/\/www.tunerankings.com\/\" target=\"_blank\">www.tunerankings.com<\/a>/, "");

                        			lyricmasterFetcher.docallback(respLyr, cbFn);
                			}
					else
                        			lyricmasterFetcher.docallback("", cbFn);
				}

				req.send(null);
			}
			else
				lyricmasterFetcher.docallback(respLyr, cbFn);
		}
		else
			lyricmasterFetcher.docallback("", cbFn);

	}
	req.send(null);
 },

 docallback: function(respLyr, cbFn)
 {
	if (respLyr.length < 10)
		cbFn("");
	else
		cbFn(respLyr);
 }
}
